package com.lixi.mc.service.impl;

import com.lixi.mc.domain.Blog;
import com.lixi.mc.service.BlogMcService;
import lombok.extern.slf4j.Slf4j;
import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

import java.util.concurrent.TimeoutException;

/**
 * @author
 */
@Slf4j
public class BlogMcServiceImpl implements BlogMcService {

    private MemcachedClient memcachedClient;

    public BlogMcServiceImpl(MemcachedClient memcachedClient) {
        this.memcachedClient = memcachedClient;
    }

    @Override
    public Boolean set(String key, Blog blog) {
        try {
            return memcachedClient.set(key, 0, blog);
        } catch (Exception e) {
            log.error("set " + key, e);
        }
        return false;
    }

    @Override
    public Boolean set(String key, Blog blog, int exptime) {
        return null;
    }

    @Override
    public Boolean delete(String key) {
        return null;
    }

    @Override
    public Blog get(String key) {
        try {
            return memcachedClient.get(key);
        } catch (Exception e) {
            log.error("get " + key, e);
            return null;
        }
    }

    @Override
    public GetsResponse<Blog> gets(String key) {
        try {
            return memcachedClient.gets(key);
        } catch (Exception e) {
            log.error("get " + key, e);
            return null;
        }
    }
    @Override
    public Boolean cas(String key, int exp, Blog value, long cas) {
        try {
            return memcachedClient.cas(key, exp, value, cas);
        } catch (TimeoutException | InterruptedException | MemcachedException e) {
            log.error("cas " + key, e);
            return null;
        }
    }
}
